<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\exception\HttpException;
use app\admin\model\Admin;

/**
 * 图文管理
 */
class Article extends Controller
{
    /**
     * 图文列表
     */
    public function list()
    {
        $articleList = Db::name('article')->where('status', 1)->select();
        $categoryList = Db::name('category')->where('status', 1)->column('name','id');
        // print_r($categoryList);exit;
        // $articleNum = Db::name('article')->where('status', 1)->count('id');
        $this->assign([
            // 'count' => $articleNum,
            'articleList' => $articleList,
            'categoryList'    => $categoryList,
        ]);
        return $this->fetch();
    }

    /**
     * 查看文章详情
     */
    public function detail()
    {
        $error = 1;
        $noError = 0;
        $params = $_GET;
        if (!isset($params['id']) || intval($params['id']) <= 0) {
            $this->assign([
                'error' => $error
            ]);
            return $this->fetch();
        }
        $id = intval($params['id']);
        $article = Db::name('article')->where('id', $id)->find();
        $categoryList = Db::name('category')->where('status', 1)->column('name','id');
        $this->assign([
            'error'   => $noError,
            'id'      => $id,
            'article' => $article,
            'categoryList' => $categoryList            
        ]);
        return $this->fetch();
    }

    /**
     * API保存图片
     */
    public function saveimg()
    {
        $data = ['code'=>1,'msg'=>'图片上传失败！'];  
        try{
            if (isset($_GET['id']) && intval($_GET['id'])>0) {
                $id = $_GET['id'];
            } else {
                throw new \Exception('缺少参数ID');
            }
            $article = Db::name('article')->where('id', $id)->find();
            if (!$article) {
                throw new \Exception("文章{$id}不存在");
            }
            $fileArr = $_FILES['files'];  
            //设置预览目录  
            $previewPath = "public/static/";  
            //检查是否为图片  
            $ext = $this->getExt($fileArr['name'][0]);  
            $arrExt = ['jpg','jpeg','gif','png'];  
            if(!in_array($ext,$arrExt)) {
                $data['code'] = 1; 
                $data['msg'] = 'Error:文件《'.$fileArr['name'][0].'》不是图片或采用了不合适的扩展名！';  
            } else {  
                //文件上传到预览目录      
                if($fileArr['error'][0] == 0) {
                    $previewName = 'home/images/'.time().'.'.$ext;  
                    $previewSrc = $previewPath.$previewName;  
                    if(!move_uploaded_file($fileArr['tmp_name'][0],$previewSrc)) {  
                        $data['sta'] = FALSE;  
                        $data['msg'] = $fileArr['name'][0].'图片上传失败！';  
                    } else {  
                        $data['msg'] = $fileArr['name'][0].'图片上传成功！';  
                        $data['previewSrc'] = $previewSrc;  
                        Db::name('article')->where('id', $id)->update(['thumb'=>$previewName]);
                    }             
                }  
            }
        } catch (\Exception $e) {
            $data['msg'] = $e->getMessage();
        }
        return json($data);
    }

    /**
     * API保存文章详情
     */
    public function save()
    {
        $id = $_POST['id'];
        $updateData = ['title'=>$_POST['title'], 'description'=>$_POST['description'], 'content'=>$_POST['content'], 'categoryid'=>$_POST['catgoryid']];
        Db::name('article')->where('id', $id)->update($updateData);
        return $this->redirect('list');
    }

    //获取文件扩展名  
    function getExt($filename) {  
        $ext = pathinfo($filename, PATHINFO_EXTENSION);  
        return $ext;  
    }  
    //创建目录并赋权限  
    // function creatDir($path) {  
    //     $arr = explode('/',$path);  
    //     $dirAll = '';  
    //     $result = FALSE;  
    //     if(count($arr) > 0) {  
    //         foreach($arr as $key=>$value) {  
    //             $tmp = trim($value);  
    //             if($tmp != '') {  
    //                 $dirAll .= $tmp.'/';  
    //                 if(!file_exists($dirAll)) {  
    //                     mkdir($dirAll,0777,true);                     
    //                 }  
    //             }  
    //         }  
    //     }  
    // }  

}